package com.sxtyxt.online.me;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sxtyxt.online.ActBase;
import com.sxtyxt.online.R;
import com.sxtyxt.online.imagecrop.ActClipImage;
import com.sxtyxt.util.DateUtil;
import com.sxtyxt.util.DialogUtil;
import com.sxtyxt.util.DialogUtil.SelectDateListener;
import com.sxtyxt.util.FileUtil;
import com.sxtyxt.util.ImageLoaderUtil;
import com.sxtyxt.util.ImageUtil;
import com.sxtyxt.widget.ActionSheetDialog;
import com.sxtyxt.widget.ActionSheetDialog.OnSheetItemClickListener;
import com.sxtyxt.widget.ActionSheetDialog.SheetItemColor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 完善资料
 *
 * @author General
 */
public class ActCompleteInfo extends ActBase implements OnClickListener {

    private LinearLayout layoutPhoto, layoutNickName, layoutSex, layoutBirthday, layoutAddress;
    private ImageView ivHeader;
    private TextView txtBirthday;
    private static final int MODIFY_SEX = 1000;
    private String sex;
    private TextView txtSex;
    private EditText etxtName, etxtAddress;

    private String uploadPath;

    @Override
    protected int getLayoutId() {
        return R.layout.complete_info;
    }

    @Override
    protected void initUI() {
        super.initUI();
        layoutPhoto = (LinearLayout) findViewById(R.id.layout_set_photo);
        layoutPhoto.setOnClickListener(this);
        layoutNickName = (LinearLayout) findViewById(R.id.layout_nickname);
        layoutNickName.setOnClickListener(this);
        layoutSex = (LinearLayout) findViewById(R.id.layout_sex);
        layoutSex.setOnClickListener(this);
        layoutBirthday = (LinearLayout) findViewById(R.id.layout_birthday);
        layoutBirthday.setOnClickListener(this);
        layoutAddress = (LinearLayout) findViewById(R.id.layout_address);
        layoutAddress.setOnClickListener(this);
        ivHeader = (ImageView) findViewById(R.id.iv_header);
        txtBirthday = (TextView) findViewById(R.id.txt_birthday);
        etxtName = (EditText) findViewById(R.id.etxt_nickname);
        etxtAddress = (EditText) findViewById(R.id.etxt_address);
        txtSex = (TextView) findViewById(R.id.txt_sex);
        Button btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(this);

        if (getLoginUser() != null) {
            etxtName.setText(getUnNullString(getLoginUser().getUserName(), ""));
            etxtName.setSelection(etxtName.getText().length());

            sex = getLoginUser().getUserSex();
            if ("1".equals(sex)) {
                txtSex.setText("男");
            } else {
                txtSex.setText("女");
            }
            txtBirthday.setText(DateUtil.longToString(Long.parseLong(getLoginUser().getBirthday() == null ? "0" : getLoginUser().getBirthday()), DateUtil.YYYYMMDD));
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_set_photo:
                selectHeadImg();
                break;
            case R.id.layout_birthday:
                DialogUtil.selectDate(this, new SelectDateListener() {

                    @Override
                    public void select(String year, String month, String day, String dateString) {

                        int result = DateUtil.compareDate(dateString, DateUtil.getCurrentDay(DateUtil.YYYYMMDD));
                        if (result == 0 || result == 1) {
                            txtBirthday.setText(dateString);
                        } else {
                            doToast("出生日期不能大于当前日期");
                        }
                    }
                });
                break;

            case R.id.layout_sex:
                transfer(ActModifySex.class, MODIFY_SEX);
                break;
            case R.id.btn_submit:
//                if (TextUtils.isEmpty(getStringByUI(etxtName))) {
//                    doToast("昵称不能为空");
//                    return;
//                }
//                if (TextUtils.isEmpty(getStringByUI(txtBirthday))) {
//                    doToast("生日不能为空");
//                    return;
//                }
//                if (TextUtils.isEmpty(sex)) {
//                    doToast("性别不能为空");
//                    return;
//                }
//                if (TextUtils.isEmpty(getStringByUI(etxtAddress))) {
//                    doToast("住址不能为空");
//                    return;
//                }
                updateMember();
                break;
            default:
                break;
        }
    }

    /**
     *
     */
    private void selectHeadImg() {
        ActionSheetDialog photoDialog = new ActionSheetDialog(this);
        photoDialog.builder();
        photoDialog.setCancelable(true);
        photoDialog.setCanceledOnTouchOutside(true);
        photoDialog.addSheetItem("拍照", SheetItemColor.Black, new OnSheetItemClickListener() {
            @Override
            public void onClick(int which) {
                takeCapture();

            }

        });
        photoDialog.addSheetItem("从相册中选择", SheetItemColor.Black, new OnSheetItemClickListener() {

            @Override
            public void onClick(int which) {
                fromLocal();
            }

        });
        photoDialog.show();
    }

    private String filePath;

    /**
     * 拍照
     */
    private void takeCapture() {

    }

    /**
     * 本地选取
     */
    private void fromLocal() {

        ImageUtil.fromLocal(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK)
            switch (requestCode) {
            case ImageUtil.CAPTUR_FROM_LOCAL:
                String uriOrPath = data.getDataString();
                if (uriOrPath.contains("file://")) {
                    uriOrPath = uriOrPath.replace("file://", "");
                    filePath = uriOrPath;
                    transfer(ActClipImage.class, filePath, "PATH", ActClipImage.CLIPIMAGE);
                    return;
                }
                Uri uri = data.getData();

                if (uri != null) {

                    Log.i("uri", uri.toString());

                    ContentResolver cr = this.getContentResolver();

                    Cursor cursor = cr.query(uri, null, null, null, null);
                    if (cursor != null) {
                        if (cursor.moveToFirst()) {
                            int dataIndex = cursor.getColumnIndex("_data");
                            int mimeIndex = cursor.getColumnIndex("mime_type");
                            if (dataIndex >= 0)
                                filePath = cursor.getString(dataIndex);
                            String mimeType = null;
                            if (mimeIndex >= 0) {
                                mimeType = cursor.getString(mimeIndex);
                            }

                            cursor.close();
                            if (filePath != null) {
                                transfer(ActClipImage.class, filePath, "PATH", ActClipImage.CLIPIMAGE);
                            }

                        }

                    }
                }

                break;
            case ImageUtil.CAPTUR_FROM_CAMERA:
                if (FileUtil.isExist(filePath)) {
                    ImageUtil.resetImageOrientation(filePath);
                    transfer(ActClipImage.class, filePath, "PATH", ActClipImage.CLIPIMAGE);

                }
                break;
            case ActClipImage.CLIPIMAGE:
                // new path
                String path = data.getStringExtra("bitmap");
                System.out.println("path:" + path);
                ImageLoader.getInstance().displayImage("file://" + path, ivHeader, ImageLoaderUtil.getCircleOption(R.drawable.img_header));
                uploadPath = path;
                break;

            case MODIFY_SEX:
                sex = data.getStringExtra("sex");
                if ("1".equals(sex)) {
                    txtSex.setText("男");
                } else {
                    txtSex.setText("女");
                }

                break;

            default:
                break;

        }

    }

    /**
     * 修改用户信息
     */
    private void updateMember() {
        List<File> files = null;
        if (!TextUtils.isEmpty(uploadPath)) {
            files = new ArrayList<File>();
            files.add(new File(filePath));
        }

    }
}
